package com.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Product;
import com.model.Shopcart;
import com.model.User;
import com.model.Workstation;
import com.service.ShopcartService;
import com.service.WorkstationService;
import com.util.Conversion;
import com.util.JdbcUtil;
import com.util.PageUtil;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/cart")
public class ShopcartServlet extends BaseServlet {
	JdbcUtil jdbc=new JdbcUtil();
	ShopcartService cartService = new ShopcartService();
	WorkstationService stationService=new WorkstationService();
	public boolean checkUserIsLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		User user = (User)request.getSession().getAttribute("user");
		if(user==null){
			String mess="暂未登录，请登录后再操作！";
			request.setAttribute("mess",mess);
			request.getRequestDispatcher("userLogin.jsp").forward(request, response);
			return false;
		}	
		return true;
	}
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		Shopcart cart = new Shopcart();
		Conversion.convert(cart, request);

		List<Shopcart> carts =null;

		carts = cartService.queryCart(cart);
		//判断查询的单条记录是否存在
		try {
			if(!carts.isEmpty()){//存在，更新记录里的重量
				cartService.updateCartByUidPid(cart.getUid(), cart.getPid(),cart.getWeight());
			}else{//不存在，插入一条新记录
				cartService.add(cart);
			}
			//更新session中用户出售页数量
			setCartCount(request,response);
			response.sendRedirect("cart?action=query");
		} catch (Exception e) {
			response.sendRedirect("error.jsp");
			e.printStackTrace();
		}
	}
	private void add_ajax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		Shopcart cart = new Shopcart();
		Conversion.convert(cart, request);
		System.out.println(cart);
		List<Shopcart> carts =null;
		carts = cartService.queryCart(cart);
		System.out.println(carts);
		//判断查询的单条记录是否存在
		try {
			if(!carts.isEmpty()){//存在，更新记录里的重量
				cartService.updateCartByUidPid(cart.getUid(), cart.getPid(),cart.getWeight());
				//System.out.println("执行更新！");
			}else{//不存在，插入一条新记录
				cartService.add(cart);
				//System.out.println("执行插入！");
			}
			//更新session中用户出售页数量
			setCartCount(request,response);
			response.getWriter().write("1");
		} catch (Exception e) {
			response.getWriter().write("0");
			e.printStackTrace();
		}
	}
	private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		Integer cp = Integer.parseInt(request.getParameter("cp")==null?"1":request.getParameter("cp"));
		Integer pagesize=100;//页面分页已去除
		Shopcart cart = new Shopcart();
		Conversion.convert(cart, request);
		User user= (User)request.getSession().getAttribute("user");
		cart.setUid(user.getUid());
		//System.out.println(cart);
		List<Shopcart> carts =null;
		carts = cartService.queryCart_show(cart, cp, pagesize);
		int count = cartService.queryCart(cart).size();
		//查询所有站点信息
		List<Workstation> workstations = stationService.selectWorkstation();
		request.setAttribute("workstations",workstations);
		
		//更新session中用户出售页数量
		setCartCount(request,response);
		
		String pageTool = PageUtil.getPageTool(request, count, cp, pagesize);
		request.setAttribute("pageTool",pageTool);
		request.setAttribute("carts",carts);
		request.getRequestDispatcher("cart.jsp").forward(request, response);
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		String sid =request.getParameter("sid");		
		try {
			cartService.deleteBySid(Integer.parseInt(sid));
			//更新session中用户出售页数量
			setCartCount(request,response);
			response.getWriter().write("1");
		} catch (Exception e1) {
			response.getWriter().write("0");
			e1.printStackTrace();
		}
	}
	private void delAllByUid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		String uid =request.getParameter("uid");
		try {
			cartService.deleteByUid(Integer.parseInt(uid));
			//更新session中用户出售页数量
			setCartCount(request,response);
			response.sendRedirect("order?action=query");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	private void updateWeight(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!checkUserIsLogin(request,response)){
			return;
		}
		String sid =request.getParameter("sid");
		String yunsuan=request.getParameter("yunsuan");
		//System.out.println(sid+"   "+yunsuan);
		try {
			cartService.updateCartWeight(Integer.parseInt(sid), yunsuan);
			response.getWriter().write("1");
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().write("0");
		}
	}
	private void setCartCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user= (User)request.getSession().getAttribute("user");
		Integer cartCount=cartService.getCartCountByUid(user.getUid());
		request.getSession().setAttribute("cartCount", cartCount);
	}
}
